# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : replace_black.py
# @Time     : 2024/2/20 0:14
# @Author   : Robot-Zsj
"""
description: 
"""


def func2(input_str: str, replace: str):
    return input_str.replace(' ', replace)


print(func2(
    input_str='1 2 3',
    replace='%$'
))

"""
思路1

如果已知空格数量，可计算出替换后字符串的长度，定义一个与之长度相等的结果列表，然后从后向前结果列表中填充字符。
"""


def func1(input_str, replace):
    replace_len = len(replace)
    count = 0
    for i in input_str:
        if i == ' ':
            count += 1
    new_len = len(input_str) + (replace_len - 1) * count
    new_string = [None for i in range(new_len)]
    point_new = new_len - 1  # 指向新字符数组最后一个
    point_origin = len(input_str) - 1  # 指向旧字符最后一个
    while 0 <= point_origin <= point_new:
        if input_str[point_origin] == ' ':
            new_string[point_new - replace_len + 1:point_new + 1] = replace
            point_new -= replace_len
        else:
            new_string[point_new] = input_str[point_origin]
            point_new -= 1
        point_origin -= 1
    return ''.join(new_string)


print(func1(
    input_str='1 2 3',
    replace='%$'
))
